<template>
  <div>
    <el-row>
      <el-col :span="12">
        <div class="grid-content bg-purple">
          <el-button type="primary" @click="add">新增</el-button>
          <el-button type="danger" @click="onDelete" :disabled="select.length==0">删除</el-button>
        </div>
      </el-col>
      <el-col :span="8" :offset="4">
        <div class="grid-content bg-purple-light">
          <el-input
            placeholder="请输入内容"
            @keyup.enter.native="onSearch"
            prefix-icon="el-icon-search"
            v-model="input2"
          ></el-input>
        </div>
      </el-col>
    </el-row>
    <div class="table">
      <el-table :data="tableData" height="300" v-loading="loading" @select="onSelectCB">
        <el-table-column type="selection" width="80"></el-table-column>
        <el-table-column prop="name" label="类别" width="150"></el-table-column>
        <el-table-column fixed prop="id" label="类别码" width="150"></el-table-column>
        <el-table-column prop="level" label="菜单级别" width="150"></el-table-column>
        <el-table-column prop="belong" label="属于" width="200"></el-table-column>
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-button size="mini" type="warning" @click="handleUpdate(scope.$index, scope.row)">修改</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      :page-size="pageSize"
      :current-page="curPage"
      @current-change="pageChange"
    ></el-pagination>

    <div class="zhezhao" v-show="showModel">
      <el-row>
        <el-col :span="10" :offset="6">
          <div class="box" style="background:#fff">
            <el-card class="box-card">
              <div slot="header" class="clearfix">
                <span>{{this.ruleForm.id?"修改":"新增"}}</span>
                <el-button style="float: right; padding: 3px 0" type="text" @click="close">关闭</el-button>
              </div>
              <div>
                <el-form
                  :model="ruleForm"
                  :rules="rules"
                  ref="ruleForm"
                  label-width="100px"
                  class="demo-ruleForm"
                >
                  <el-form-item label="类别" prop="name">
                    <el-input v-model="ruleForm.name" name="name"></el-input>
                  </el-form-item>
                  <el-form-item label="级别" prop="level">
                    <el-radio-group v-model="ruleForm.level">
                      <el-radio label="一级菜单" @change="isShow=false" name="level"></el-radio>
                      <el-radio label="二级菜单" @change="isShow=!isShow" name="level"></el-radio>
                    </el-radio-group>
                  </el-form-item>

                  <el-form-item label="父级菜单" prop="belong" v-if="isShow">
                    <el-select v-model="ruleForm.belong" placeholder="请选择活动区域" name="belong">
                      <el-option
                        v-for="(item,i) in typeData"
                        :key="i"
                        :label="item.name"
                        :value="item.name"
                      ></el-option>
                    </el-select>
                  </el-form-item>

                  <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                    <el-button @click="resetForm()">重置</el-button>
                  </el-form-item>
                </el-form>
              </div>
            </el-card>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import axios from "axios";
import qs from "qs";
import { typeUrl, typeUrl1 } from "../utile/Url";

export default {
  data() {
    return {
      tableData: [],
      typeData: [],
      select: [],
      showModel: false,
      input2: "",
      rowId: "",
      isShow: false,
      total: 0,
      pageSize: 6,
      curPage: 1,
      loading: true,
      ruleForm: {
        name: "",
        level: "一级菜单",
        belong: "",
        id: ""
      },
      rules: {
        name: [{ required: true, message: "请输入类别", trigger: "blur" }],
        level: [{ required: true, message: "请选择级别", trigger: "change" }],
        belong: [
          { required: true, message: "请选择父级菜单", trigger: "change" }
        ]
      }
    };
  },
  methods: {
    //筛选出级别是一级菜单的数据
    get1Data() {
      axios({
        url: typeUrl,
        method: "get",
        params: {
          transform: 1,
          filter: `level,cs,一级菜单`
        }
      }).then(res => {
        this.typeData = res.data.type;
      });
    },
    //获取数据
    getdata() {
      this.loading = true;
      axios({
        url: typeUrl,
        method: "get",
        params: {
          transform: 1,
          page: `${this.curPage},${this.pageSize}`,
          order: `id,desc`
        }
      }).then(res => {
        // console.log(res)
        this.tableData = res.data.type;
        this.total = res.data._results;
        this.loading = false;
      });
    },

    //当点击增加按钮时
    add() {
      this.showModel = true;
      this.ruleForm = {};
      this.ruleForm.level = "一级菜单";
      this.get1Data();
    },
    //当点击关闭按钮时
    close() {
      this.showModel = false;
      this.resetForm();
      // this.isShow=false
      this.ruleForm = {
        level: "一级菜单"
      };
      this.getdata();
    },
    //点击重置按钮时
    resetForm() {
      this.$refs["ruleForm"].resetFields();
      this.isShow = false;
    },
    //点击提交按钮时
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          axios({
            // headers: { "Content-Type": "application/x-www-form-urlencoded" },
            // url: typeUrl1,
            url: this.ruleForm.id ? `${typeUrl}/${this.ruleForm.id}` : typeUrl,
            method: this.ruleForm.id ? "put" : "post",
            // data: qs.stringify(this.ruleForm)
            data: this.ruleForm
          }).then(res => {
            // console.log(res);
            this.getdata();
            this.showModel = false;
            this.resetForm()
          });
        } 
      });
    },
    //当点击表格内删除按钮时
    handleDelete(index, row) {
      // console.log(index,row)
      if (!confirm("确定要删除吗？")) {
        return;
      }
      axios({
        url: typeUrl + "/" + row.id,
        method: "delete",
        data: row
      }).then(res => {
        this.getdata();
      });
    },
    //选择多选框时
    onSelectCB(selection, row) {
      this.select = selection;
    },
    //点击外部删除按钮
    onDelete() {
      if (confirm("确定删除吗")) {
        let ids = this.select.map(item => item.id);
        axios({
          url: typeUrl + "/" + ids.join(","),
          method: "delete"
        }).then(res => {
        this.getdata();
        this.select = [];
      });
      }
    },
    //当点击修改按钮时
    handleUpdate(index, row) {
      this.resetForm();
      this.showModel = true;
      this.ruleForm = row;
      this.rowId = row.id;
      if (this.ruleForm.level == "二级菜单") {
        this.isShow = true;
      } else {
        this.isShow = false;
      }
      /* axios({
        url:typeUrl+"/"+this.rowId,
        method:"put",
        data:this.ruleForm
      }).then(res=>{
        this.getdata()
      }).catch(r=>{
        console.log(r)
      }) */
    },
    //点击分页获取当前页
    pageChange(currentpage) {
      this.curPage = currentpage;
      this.getdata();
    },
    //搜索
    onSearch() {
      axios({
        url: typeUrl,
        method: "get",
        params: {
          transform: 1,
          page: `${this.curPage},${this.pageSize}`,
          order: `id,desc`,
          filter: `name,cs,` + this.input2
        }
      }).then(res => {
        this.tableData = res.data.type;
        this.total = res.data._results;
      });
    }
  },
  mounted() {
    this.getdata();
    this.get1Data();
  }
};
</script>

<style>
.table {
  margin: 20px 0;
}
.zhezhao {
  position: fixed;
  top: 0;
  right: 0;
  left: 0;
  bottom: 0;
  background: rgb(99, 97, 97);
  z-index: 99;
}
.box {
  margin: 15px 0;
}
</style>
